package kr.pe.smallhope.mobismap.utils.xml;



import gun.pe.kr.map.core.model.mapdata.ObjectFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;



public class XMLObjectForGMapInfo extends ObjectFactory
{
    public static JAXBContext getJAXBContext() throws Exception 
    { 
    	return JAXBContext.newInstance("gun.pe.kr.map.core.model.googlemapdata", ObjectFactory.class.getClassLoader());
    }

    public static JAXBContext getJAXBContext(String contextPath) throws Exception 
    {
    	return JAXBContext.newInstance(contextPath, ObjectFactory.class.getClassLoader());
    }

    private static Marshaller _createMarshaller() throws Exception 
    { 
    	return getJAXBContext().createMarshaller(); 
    }

    private static Marshaller _createMarshaller(String contextPath) throws Exception 
    { 
    	return getJAXBContext(contextPath).createMarshaller(); 
    }

    private static Unmarshaller _createUnmarshaller() throws Exception 
    { 
    	return getJAXBContext().createUnmarshaller(); 
    }

    private static Unmarshaller _createUnmarshaller(String contextPath) throws Exception 
    { 
    	return getJAXBContext(contextPath).createUnmarshaller(); 
    }

    public static Object unmarshal(String fileContents, String encoding) throws Exception
	{ 
    	
    	try
		{
    		Unmarshaller unmarshaller = _createUnmarshaller();
    		
		    return unmarshaller.unmarshal(new ByteArrayInputStream(fileContents.getBytes(encoding)));
		}
		catch (Exception e)
		{ 
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshal " + e.getMessage());
		}
	
	}

    public static Object unmarshalWithContextPath(String contextPath, String fileContents, String encoding) throws Exception
	{ 
    	try
		{
    		Unmarshaller unmarshaller = _createUnmarshaller(contextPath);
		    return unmarshaller.unmarshal(new ByteArrayInputStream(fileContents.getBytes(encoding)));
		}
		catch (Exception e)
		{ 
		
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshalWithContextPath " + e.getMessage());
		}
		
	}

    public static Object unmarshal(String fileContents) throws Exception
	{ 
    	try
		{
    		Unmarshaller unmarshaller = _createUnmarshaller();
    		
		    return unmarshaller.unmarshal(new ByteArrayInputStream(fileContents.getBytes("UTF-8")));
		}
		catch (Exception e)
		{ 
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshal " + e.getMessage());
		}
	
	}

    public static Object unmarshalWithContextPath(String contextPath, String fileContents) throws Exception
	{ 
    	try
		{
    		Unmarshaller unmarshaller = _createUnmarshaller(contextPath);
    		
		    return unmarshaller.unmarshal(new ByteArrayInputStream(fileContents.getBytes("UTF-8")));
		}
		catch (Exception e)
		{ 
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshalWithContextPath " + e.getMessage());
		}
		
	}

    public static Object unmarshal(byte[] fileContents) throws Exception
	{ 
    	
    	try
		{
    		Unmarshaller unmarshaller = _createUnmarshaller();
    		
		    return unmarshaller.unmarshal(new ByteArrayInputStream(fileContents));
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshal " + e.getMessage());
		}
		
	}

    public static Object unmarshalWithContextPath(String contextPath, byte[] fileContents) throws Exception
	{ 
    	
    	try
		{
    		Unmarshaller unmarshaller = _createUnmarshaller(contextPath);
    		
		    return unmarshaller.unmarshal(new ByteArrayInputStream(fileContents));
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshalWithContextPath " + e.getMessage());
		}
		
	}

    public static Object unmarshal(InputStream fileInputStream) throws Exception
	{
    	
    	try
		{
		    return _createUnmarshaller().unmarshal(fileInputStream);
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshal " + e.getMessage());
		}
	
	}

    public static Object unmarshalWithContextPath(String contextPath, InputStream fileInputStream) throws Exception
	{
    	
    	try
		{
		    return _createUnmarshaller(contextPath).unmarshal(fileInputStream);
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshalWithContextPath " + e.getMessage());
		}
		
	}

	public static Object unmarshal(File fileContents) throws Exception
	{
    	try
		{
		    return _createUnmarshaller().unmarshal(fileContents);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshal " + e.getMessage());
		}
		
	}

	public static Object unmarshalWithContextPath(String contextPath, File fileContents) throws Exception
	{
    	
    	try
		{
		    return _createUnmarshaller(contextPath).unmarshal(fileContents);
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] unmarshalWithContextPath " + e.getMessage());
		}
		
	}

	public static byte[] marshal(Object contents, String encoding) throws Exception
	{
    	
    	try
		{
			ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
			Marshaller marshaller = _createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
			marshaller.marshal(contents, resultStream);
			
			return resultStream.toByteArray();
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] marshal " + e.getMessage());
		}
		
	}

	public static byte[] marshalWithContextPath(String contextPath, Object contents, String encoding) throws Exception
	{
    	
    	try
		{
			ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
			Marshaller marshaller = _createMarshaller(contextPath);
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
			marshaller.marshal(contents, resultStream);
			
			return resultStream.toByteArray();
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] marshalWithContextPath " + e.getMessage());
		}
		
	}

	public static byte[] marshal(Object contents) throws Exception
	{
    	
    	try
		{
			ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
			Marshaller marshaller = _createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.marshal(contents, resultStream);
			
			return resultStream.toByteArray();
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] marshal " + e.getMessage());
		}
		
	}

	public static byte[] marshalWithContextPath(String contextPath, Object contents) throws Exception
	{
    	
    	try
		{
			ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
			Marshaller marshaller = _createMarshaller(contextPath);
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.marshal(contents, resultStream);
			
			return resultStream.toByteArray();
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] marshalWithContextPath " + e.getMessage());
		}
		
	}

	public static void marshal(Object contents, FileOutputStream fileOutStream) throws Exception
	{
    	try
		{
			Marshaller marshaller = _createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.marshal(contents, fileOutStream);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			
			throw new Exception("[XMLObject] marshal " + e.getMessage());
		}
		
	}

	public static void marshalWithContextPath(String contextPath, Object contents, FileOutputStream fileOutStream) throws Exception
	{
    	
    	try
		{
			Marshaller marshaller = _createMarshaller(contextPath);
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.marshal(contents, fileOutStream);
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
			
			throw new Exception("[XMLObject] marshalWithContextPath " + e.getMessage());
		}
		
	}

	public DataInputStream getInputStream(String fileNameWithPath)
	{
		return new DataInputStream(this.getClass().getClassLoader().getResourceAsStream(fileNameWithPath));
	}
}
